﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.DataControls;

public partial class Templates_BrochureHolder_Default : Acms.AcmsControls.TemplatePage
{


    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsList.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsList_SelectData);

        dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlFeed_ItemDataBound);

        dsSibling.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsSibling_SelectData);
        dlNavSiblings.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavSiblings_ItemDataBound);
        //dlFeed.ItemDataBound+= new EventHandler<Acms.WebControls.AcmsDataList>(dlFeed_ItemDataBound);
        //dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.AcmsDataListItemEventArgs>(dlFeed_ItemDataBound); 

        //base.DataBind();
        //GetListData();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        pnlCheckAllCheckBoxList.Visible = false;
        if (!IsPostBack)
        {
            DataBind();
            pnlCheckAllCheckBoxList.Visible = true;
        }

    }

    void dlFeed_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnkThumb = e.Item.FindControl("lnkThumb") as HyperLink;
            //var lnkDownload = e.Item.FindControl("lnkDownload") as HyperLink;

            if (lnkThumb != null)
            {
                lnkThumb.NavigateUrl = (AcmsContext.FullyQualifiedUrl(post.Url, this)) + "?&keepThis=true&TB_iframe=true&height=520&width=880&host=";

            }
        }
    }

   

    protected void btnApplyFilter_Click(object sender, EventArgs e)
    {
        List<int> languageFilterIds = new List<int>();

        List<Topic> languages = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.CurrentPost.SiteId, "BrochureLanguage");
        for (int i = 0; i < cblLanguages.Items.Length; i++)
        {
            if (cblLanguages.Items[i].Checked)
            {
                languageFilterIds.Add(languages[i].Id);
            }
        }
        LanguageFilterIds = languageFilterIds.ToArray();

        List<int> productFilterIds = new List<int>();
        List<Topic> products = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.CurrentPost.SiteId, "BrochureProduct");
        for (int i = 0; i < cblProducts.Items.Length; i++)
        {
            if (cblProducts.Items[i].Checked)
            {
                productFilterIds.Add(products[i].Id);
            }
        }
        ProductFilterIds = productFilterIds.ToArray();
        dlFeed.DataBind();
        //ucFeedModule.Rebind();
    }

    


    int[] ProductFilterIds;
    int[] LanguageFilterIds;

    

    void dsList_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        List<Post> prodData = null;
        List<Post> languageData = null;

        if (ProductFilterIds != null && ProductFilterIds.Length > 0)
        {
            prodData = Post.GetAllByTopicIdsAndParentPath(AcmsContext.SiteId, ProductFilterIds, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
        }
        if (LanguageFilterIds != null && LanguageFilterIds.Length > 0)
        {
            languageData = Post.GetAllByTopicIdsAndParentPath(AcmsContext.SiteId, LanguageFilterIds, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
        }

        //if both need to find posts in both
        if (prodData != null && languageData != null)
        {
            var langPostIDs = languageData.Select(x => x.Id);
            var commonPosts = prodData.Where(x => langPostIDs.Contains(x.Id)).ToList();
            e.SetData(commonPosts);
        }
        //else if(prodData != null)
        //{
        //    e.SetData(prodData);
        //}
        //else if (languageData != null)
        //{
        //    e.SetData(languageData);
        //}
        else
        {
            if (txtFirstFilter.Value == "0")
            {
                e.SetData(AcmsContext.CurrentPost.Children); 
                txtFirstFilter.Value = "1";
            }
            else
            {
                //e.SetData(null);
            }
         
            
        }
    }

    public void Rebind()
    {
        dlFeed.DataBind();
    }

    protected override void DataBindChildren()
    {
        cblProducts.DataSource = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.SiteId, "BrochureProduct");
        cblLanguages.DataSource = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.SiteId, "BrochureLanguage");
        //accordianFilterPane.DataBind();
        base.DataBindChildren();
    }

    protected override object SaveViewState()
    {
        ViewState["ProductFilterIds"] = ProductFilterIds;
        ViewState["LanguageFilterIds"] = LanguageFilterIds;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        ProductFilterIds = (int[])ViewState["ProductFilterIds"];
        LanguageFilterIds = (int[])ViewState["LanguageFilterIds"];
    }
    void dsSibling_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }
    void dlNavSiblings_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNav") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }
}